package com.wits.qso.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.platform.entity.User;
import com.wits.qso.dao.custom.VmUserDaoCustom;
import com.wits.qso.entity.Vm;
import com.wits.qso.entity.VmUser;

@Component
public class VmUserDaoImpl extends BaseDaoImpl<VmUser, String> implements VmUserDaoCustom
{

	@Override
	public List<VmUser> findByClassIdAndLimit(String clsId, int limit) {
		String sql="select vu.* from qso_vm_user vu inner join qso_student s on(vu.user_id=s.stu_userid) where s.cls_id=? limit %d";
		return this.findByNative(VmUser.class,String.format(sql,limit), clsId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VmUser> findByUserId(List<String> userIdList) {
		
		String hql="select vu from VmUser vu where vu.user.userId in :idList";
		Query query=em.createQuery(hql);
		query.setParameter("idList", userIdList);
		return query.getResultList();

	}

	@Override
	public VmUser findByUserId(String userId) {
		String hql="select vu from VmUser vu where vu.user.userId=:userId";
		Query query=em.createQuery(hql);
		query.setParameter("userId", userId);
		List<VmUser> list=query.getResultList();
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	public List<User> findExistedUser() {
		return (List<User>) em.createQuery( "SELECT o.user FROM VmUser o" ).getResultList();
	}
	
	public List<Vm> findExistedVm() {
		return (List<Vm>) em.createQuery( "SELECT o.vm FROM VmUser o" ).getResultList();
	}
}
